package com.ba.baseandroid

import android.content.Context
import android.content.Intent
import com.alibaba.android.arouter.launcher.ARouter
import com.ba.base.api.ApiAuth
import com.ba.base.utils.SharedPreUtils
import okhttp3.Interceptor
import okhttp3.Response
import qsos.lib.base.utils.ActivityManager

/**
 * 登录异常拦截器
 */
class LoginAbnormalInterceptor(private var mContext: Context) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val originResponse = chain.proceed(request)
        if (originResponse.code() == 401) { // 未登录
//            val intent = Intent(mContext, LoginActivity::class.java)
//            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
//            mContext.startActivity(intent)
//            loginOut(mContext)
//            ActivityManager.finishAll()
        }
        return originResponse
    }
    private fun loginOut(context: Context) {
        clear(context)
        SharedPreUtils.clearUserInfo(context)
        SharedPreUtils.clear(context)
        ARouter.getInstance().build("/mine/login").navigation()
    }

    private fun clear(context: Context) {
        SharedPreUtils.clearUserInfo(context)
        ApiAuth.status = ApiAuth.STATUS.NULL
        ApiAuth.bearer = ""
        ApiAuth.userId = ""
        ApiAuth.deptId = -1
        ApiAuth.phone = ""
        ApiAuth.user = null
    }
}